package com.lyh.crm.workbench.service.impl;

import com.lyh.crm.settings.dao.UserDao;
import com.lyh.crm.settings.domain.User;
import com.lyh.crm.utils.DateTimeUtil;
import com.lyh.crm.utils.SqlSessionUtil;
import com.lyh.crm.utils.UUIDUtil;
import com.lyh.crm.vo.PaginationVO;
import com.lyh.crm.workbench.dao.ContactsActivityRelationDao;
import com.lyh.crm.workbench.dao.ContactsDao;
import com.lyh.crm.workbench.dao.ContactsRemarkDao;
import com.lyh.crm.workbench.dao.CustomerDao;
import com.lyh.crm.workbench.domain.*;
import com.lyh.crm.workbench.service.ContactsService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContactsServiceImpl implements ContactsService {

    private ContactsDao contactsDao = SqlSessionUtil.getSqlSession().getMapper(ContactsDao.class);
    private ContactsRemarkDao contactsRemarkDao = SqlSessionUtil.getSqlSession().getMapper(ContactsRemarkDao.class);
    private ContactsActivityRelationDao contactsActivityRelationDao = SqlSessionUtil.getSqlSession().getMapper(ContactsActivityRelationDao.class);

    private CustomerDao customerDao = SqlSessionUtil.getSqlSession().getMapper(CustomerDao.class);

    private UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);

    public List<Contacts> getContactsListByName(Map<String, Object> map) {
        List<Contacts> cList = contactsDao.getContactsListByName(map);

        return cList;
    }

    public boolean changeCustomerId(Tran t) {
        boolean flag = true;
        System.out.println("t = " + t);
        Map<String,String> map = new HashMap<String, String>();
        map.put("contactsId",t.getContactsId());
        map.put("customerId",t.getCustomerId());
        map.put("editBy",t.getEditBy());
        map.put("editTime",t.getEditTime());
        int count = contactsDao.changeCustomerId(map);

        if (count != 1) {
            flag = false;
        }

        return flag;
    }

    public boolean save(Contacts con) {

        boolean flag = true;

        String customerName = con.getCustomerId();
        Customer cus = customerDao.getCustomerByName(customerName);


        //如果cus为null，需要创建客户
        if (cus == null) {

            cus = new Customer();
            cus.setId(UUIDUtil.getUUID());
            cus.setName(customerName);
            cus.setCreateBy(con.getCreateBy());
            cus.setCreateTime(DateTimeUtil.getSysTime());
            cus.setContactSummary(con.getContactSummary());
            cus.setNextContactTime(con.getNextContactTime());
            cus.setOwner(con.getOwner());
            //添加客户
            int count1 = customerDao.save(cus);
            if (count1 != 1) {
                flag = false;
            }

        }

        //通过以上对于客户的处理，不论是查询出来已有的客户，还是以前没有我们新增的客户，总之客户已经有了，客户的id就有了
        //将客户id封装到con对象中
        System.out.println("customer = " + cus.getId());
        con.setCustomerId(cus.getId());

        int count = contactsDao.save(con);
        System.out.println("count = " + count);
        if (count != 1) {

            flag = false;

        }

        return flag;
    }

    public PaginationVO<Contacts> pageList(Map<String, Object> map) {
        //取得total
        int total = contactsDao.getTotalByCondition(map);
        //取得dataList
        List<Contacts> dataList = contactsDao.getContactsListByCondition(map);

        //创建一个vo对象，将total和dataList封装到vo中
        PaginationVO<Contacts> vo = new PaginationVO<Contacts>();
        vo.setTotal(total);
        vo.setDataList(dataList);

        //将vo返回
        return vo;
    }

    public boolean deleteByIds(String[] ids) {

        boolean flag = true;

        //查询出需要删除的备注的数量
        int count1 = contactsRemarkDao.getCountByAids(ids);

        //删除备注，返回受到影响的条数（实际删除的数量）
        int count2 = contactsRemarkDao.deleteByAids(ids);

        if(count1!=count2){

            flag = false;

        }

        //删除联系人
        int count = contactsDao.deleteByIds(ids);
        if (count != ids.length) {

            flag = false;

        }

        return flag;
    }

    public Map<String, Object> getUserListAndContacts(String id) {
        //取uList
        List<User> uList = userDao.getUserList();

        //取con
        Contacts con = contactsDao.getById(id);
        System.out.println(con);
        //将uList和con打包到map中
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("uList", uList);
        map.put("con", con);

        //返回map就可以了
        return map;
    }

    public boolean update(Contacts con) {
        boolean flag = true;

        String customerName = con.getCustomerId();
        System.out.println("customerName = " + customerName);

        int count2 = customerDao.getCustomerTotalId(customerName);
        System.out.println("count2 = " + count2);

        if (count2 != 1) {

            flag = false;

        } else {

            String customerId = customerDao.getCustomerId(customerName);
            System.out.println("customerId = " + customerId);
            con.setCustomerId(customerId);

            int count = contactsDao.update(con);
            if (count != 1) {

                flag = false;

            }
        }
        return flag;
    }

    public boolean deleteById(String id) {

        boolean flag = true;

        //删除
        int count = contactsDao.deleteById(id);
        if (count != 1) {

            flag = false;

        }

        return flag;
    }

    public Contacts detail(String id) {
        Contacts con = contactsDao.detail(id);

        return con;
    }

    public List<ContactsRemark> getRemarkListByAid(String contactsId) {

        List<ContactsRemark> crList = contactsRemarkDao.getRemarkListByAid(contactsId);

        return crList;
    }

    public boolean deleteRemark(String id) {

        boolean flag = true;

        int count = contactsRemarkDao.deleteById(id);

        if(count!=1){

            flag = false;

        }

        return flag;
    }

    public boolean saveRemark(ContactsRemark cr) {

        boolean flag = true;

        int count = contactsRemarkDao.saveRemark(cr);

        if(count!=1){

            flag = false;

        }

        return flag;
    }

    public boolean updateRemark(ContactsRemark cr) {

        boolean flag = true;

        int count = contactsRemarkDao.updateRemark(cr);

        if(count!=1){

            flag = false;

        }

        return flag;
    }

    public boolean unbund(String id) {

        boolean flag = true;

        int count = contactsActivityRelationDao.unbund(id);

        if (count != 1) {

            flag = false;

        }

        return flag;
    }

    public boolean bund(String contactsId, String[] aids) {

        boolean flag = true;

        for (String aid : aids) {

            //取得每一个aid和cid做关联
            ContactsActivityRelation car = new ContactsActivityRelation();
            car.setId(UUIDUtil.getUUID());
            car.setActivityId(aid);
            car.setContactsId(contactsId);

            //添加关联关系表中的记录
            int count = contactsActivityRelationDao.bund(car);
            if (count != 1) {
                flag = false;
            }

        }

        return flag;
    }

    public List<Contacts> getContactsByCustomerName(String customerName) {

        List<Contacts> conList = contactsDao.getContactsByCustomerName(customerName);

        return conList;
    }


}
